/**
 * @Author your name
 * @Date 2021-03-04 09:53:23
 * @LastEditTime 2021-03-04 10:07:50
 * @LastEditors Please set LastEditors
 * @Description In User Settings Edit
 * @FilePath \fed-e-task-04-06\code\angular-route\src\app\guards\unsave.guard.ts
 */
import { Injectable } from '@angular/core';
import { CanDeactivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';

export interface CanLeave {
  canLeave: () => boolean
}

@Injectable({
  providedIn: 'root'
})
export class UnsaveGuard implements CanDeactivate<CanLeave> {
  canDeactivate(
    component: CanLeave,
    currentRoute: ActivatedRouteSnapshot,
    currentState: RouterStateSnapshot,
    nextState?: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    if (component.canLeave()) {
      return true
    } else {
      if (confirm('确定离开吗？')) {
        return true
      } else {
        return false
      }
    }
  }
}
